package com.lefevre.cms.service.like.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lefevre.cms.bean.QueryResult;
import com.lefevre.cms.bean.like.Like;
import com.lefevre.cms.bean.like.TopicLike;
import com.lefevre.cms.mapper.LikeMapper;
import com.lefevre.cms.mapper.TopicLikeMapper;
import com.lefevre.cms.service.like.LikeService;
import net.sf.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 点赞
 *
 * @author Lefevre
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Resource
    private TopicLikeMapper topicLikeMapper;

    /**
     * 根据Id查询点赞
     *
     * @param likeId 点赞Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Like findById(String likeId) {
        return baseMapper.selectById(likeId);
    }

    /**
     * 根据用户名称查询点赞分页
     *
     * @param userId     用户Id
     * @param userName   用户名称
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Like> findLikeByUserId(Long userId, String userName, int firstIndex, int maxResult) {
        QueryResult<Like> qr = new QueryResult<>();

        LambdaQueryWrapper<Like> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Like::getUserName, userName);
        wrapper.orderByDesc(Like::getAddtime);
        Page<Like> likePage = baseMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        qr.setResultlist(likePage.getRecords());
        qr.setTotalrecord(likePage.getTotal());
        return qr;
    }


    /**
     * 保存点赞
     *
     * @param like      点赞
     * @param topicLike 话题点赞
     */
    public void saveLike(Object like, Object topicLike) {
        baseMapper.insert((Like) like);
        topicLikeMapper.insert((TopicLike) topicLike);
    }

    /**
     * 删除点赞
     *
     * @param likeId      点赞Id
     * @param topicLikeId 话题点赞Id
     */
    public Integer deleteLike(String likeId, String topicLikeId) {
        int i = baseMapper.deleteById(likeId);
        i += topicLikeMapper.deleteById(likeId);
        return i;
    }

    /**
     * 根据话题Id删除点赞
     *
     * @param topicId 话题Id
     */
    public Integer deleteLikeByTopicId(Long topicId) {
        int j = baseMapper.delete(new LambdaUpdateWrapper<Like>().eq(Like::getTopicId, topicId));
        j += topicLikeMapper.delete(new LambdaUpdateWrapper<TopicLike>().eq(TopicLike::getTopicId, topicId));
        return j;
    }

    /**
     * 根据点赞用户名称删除点赞
     *
     * @param userNameList 用户名称集合
     */
    public Integer deleteLikeByUserName(List<String> userNameList) {
        int j = baseMapper.delete(new LambdaUpdateWrapper<Like>().in(Like::getUserName, userNameList));
        j += topicLikeMapper.delete(new LambdaUpdateWrapper<TopicLike>().in(TopicLike::getUserName, userNameList));
        return j;
    }

    /**
     * 根据发布话题的用户名称删除点赞
     *
     * @param userNameList 发布话题的用户名称集合
     */
    public Integer deleteLikeByPostUserName(List<String> userNameList) {
        int j = baseMapper.delete(new LambdaUpdateWrapper<Like>().in(Like::getPostUserName, userNameList));
        j += topicLikeMapper.delete(new LambdaUpdateWrapper<TopicLike>().in(TopicLike::getPostUserName, userNameList));
        return j;
    }


    /**
     * 根据Id查询话题点赞
     *
     * @param topicLikeId 话题点赞Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public TopicLike findTopicLikeById(String topicLikeId) {
        return topicLikeMapper.selectById(topicLikeId);
    }


    /**
     * 根据话题Id查询点赞分页
     *
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @param topicId    话题Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Like> findLikePageByTopicId(int firstIndex, int maxResult, Long topicId) {
        QueryResult<Like> qr = new QueryResult<>();

        LambdaQueryWrapper<TopicLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TopicLike::getTopicId, topicId);
        wrapper.orderByDesc(TopicLike::getAddtime);
        Page<TopicLike> topicLikePage = topicLikeMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        List<TopicLike> topicLikeList = topicLikePage.getRecords();
        List<Like> likeList = new ArrayList<>();
        if (topicLikeList != null && topicLikeList.size() > 0) {
            BeanCopier copier = BeanCopier.create(TopicLike.class, Like.class, false);
            for (TopicLike topicLike : topicLikeList) {
                Like like = new Like();
                copier.copy(topicLike, like, null);
                likeList.add(like);
            }
        }
        qr.setResultlist(likeList);
        qr.setTotalrecord(topicLikePage.getTotal());

        return qr;
    }

    /**
     * 根据话题Id查询被点赞数量
     *
     * @param topicId 话题Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Long findLikeCountByTopicId(Long topicId) {
        return topicLikeMapper.selectCount(new LambdaQueryWrapper<TopicLike>().eq(TopicLike::getTopicId, topicId));
    }

}
